package com.mycompany.webapp.action;

import java.util.List;

import javax.persistence.Transient;

import com.mycompany.model.Invoice;
import com.mycompany.service.InvoiceManager;
import com.opensymphony.xwork2.ActionSupport;


public class AjaxAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	@Transient
	private InvoiceManager invoiceManager;
	private int page = 0;
	private List<Invoice> gridModel;
	private int rows = 0;
	

	
	@Override	
	public String execute() {
		loadGridModel();
		return SUCCESS;
	}

	public void loadGridModel() {		
		gridModel = invoiceManager.getInvoices(page, rows);
	}
	
	/*public String getJSON(){
		//loadGridModel();
	    return execute();
	}*/
	
	public int getPage() {
		if (page == 0) {
			return 1;
		} else {
			return page;
		}
	}

	public void setPage(int page) {
		this.page = page;
	}

	//Total number of pages
	public int getTotal() {
		return (getRecords()/getRows())+1;				
	}

	//Total number of records
	public int getRecords() {
		return invoiceManager.getTotalInvoices();
	}
	
	public List<Invoice> getGridModel() {
		return gridModel;
	}

	public void setGridModel(List<Invoice> gridModel) {
		this.gridModel = gridModel;
	}

	public void setInvoiceManager(InvoiceManager invoiceManager) {
		this.invoiceManager = invoiceManager;
	}

	//rowNum in grid
	public int getRows() {
		if (rows == 0) {
			return 5;
		} else {
			return rows;
		}
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	
}
